package com.stella.ieproductivitybackend.service.impl;

import com.stella.ieproductivitybackend.entity.*;
import com.stella.ieproductivitybackend.mapper.ModelMapper;
import com.stella.ieproductivitybackend.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 型体页服务层
 */
@Service
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelMapper modelMapper;

    /**
     * 工厂列表
     */
    public List<Factory> getFactoryList(String model) {
        return modelMapper.getFactoryList(model);
    }

    /**
     * 品牌列表
     */
    public List<Brand> getBrandList(String model) {
        return modelMapper.getBrandList(model);
    }

    /**
     * 型体列表
     */
    public List<Model> getModelList() {
        return modelMapper.getModelList();
    }

    /**
     * 搜索型体
     */
    public List<Model> searchModel(String model) {
        return modelMapper.searchModel(model);
    }

    /**
     * 纯粹达成率选项
     */
    public Map<String, Object> getPureOption(String model) {
        Map<String, Object> map = new HashMap<>();

        List<String> factoryList = modelMapper.getPureFactory(model);
        List<String> yearList = modelMapper.getPureYear(model);
        List<String> monthList = modelMapper.getPureMonth(model);

        map.put("factoryList", factoryList);
        map.put("yearList", yearList);
        map.put("monthList", monthList);

        return map;
    }

    /**
     * 纯粹达成率详情
     */
    public List<ModelPure> getPureDetail(String model, String factory, String year, String month) {
        return modelMapper.getPureDetail(model, factory, year, month);
    }

    /**
     * 纯粹达成率图表
     */
    public Map<String, Object> getPureChart(String model) {
        Map<String, Object> map = new HashMap<>();

        List<String> factory = new ArrayList<>();
        List<Integer> thisPure = new ArrayList<>();
        List<Integer> lastPure = new ArrayList<>();
        Map<String, Map<String, ModelPure>> operation = new LinkedHashMap<>();

        int year = Calendar.getInstance().get(Calendar.YEAR);

        List<ModelPure> pureChart = modelMapper.getPureChart(model);

        for (ModelPure chart : pureChart) {
            if (!factory.contains(chart.getFactory())) {
                factory.add(chart.getFactory());
            }

            if (Integer.parseInt(chart.getYear()) == year) {
                if (operation.get(chart.getFactory()) != null) {
                    operation.get(chart.getFactory()).put("thisPure", chart);
                } else {
                    Map<String, ModelPure> thisPureMap = new LinkedHashMap<>();
                    thisPureMap.put("thisPure", chart);
                    operation.put(chart.getFactory(), thisPureMap);
                }
            } else if (Integer.parseInt(chart.getYear()) == year - 1) {
                if (operation.get(chart.getFactory()) != null) {
                    operation.get(chart.getFactory()).put("lastPure", chart);
                } else {
                    Map<String, ModelPure> lastPureMap = new LinkedHashMap<>();
                    lastPureMap.put("lastPure", chart);
                    operation.put(chart.getFactory(), lastPureMap);
                }
            }
        }

        for (Map.Entry<String, Map<String, ModelPure>> entry : operation.entrySet()) {
            if (entry.getValue().get("thisPure") != null) {
                thisPure.add(entry.getValue().get("thisPure").getRate6());
            } else {
                thisPure.add(0);
            }
            if (entry.getValue().get("lastPure") != null) {
                lastPure.add(entry.getValue().get("lastPure").getRate6());
            } else {
                lastPure.add(0);
            }
        }

        map.put("factory", factory);
        map.put("thisPure", thisPure);
        map.put("lastPure", lastPure);
        map.put("operation", operation);

        return map;
    }

    /**
     * 入库标准秒值图表
     */
    @Override
    public Map<String, Object> getWarehousingChart(String model) {
        Map<String, Object> map = new HashMap<>();

        List<String> factory = new ArrayList<>();
        List<Integer> target = new ArrayList<>();
        List<Integer> real = new ArrayList<>();
        Map<String, WarehousingChart> operation = new HashMap<>();

        List<WarehousingChart> warehousingChart = modelMapper.getWarehousingChart(model);

        for (WarehousingChart chart : warehousingChart) {
            factory.add(chart.getFactory());
            target.add(chart.getTot_std_time1());
            real.add(chart.getTot_std_time2());
            operation.put(chart.getFactory(), chart);
        }

        map.put("factory", factory);
        map.put("target", target);
        map.put("real", real);
        map.put("operation", operation);

        return map;
    }

    /**
     * 入库标准秒值详情
     */
    @Override
    public List<Warehousing> getWarehousingDetail(String model, String year, String month) {
        return modelMapper.getWarehousingDetail(model, year, month);
    }

    /**
     * 标准秒值选项
     */
    @Override
    public Map<String, Object> getStandardOption(String model) {
        Map<String, Object> map = new HashMap<>();

        List<String> factoryList = modelMapper.getStandardFactory(model);
        List<String> revisionList = modelMapper.getStandardRevision(model);
        List<String> dmaterialList = modelMapper.getStandardDmaterial(model);

        map.put("factoryList", factoryList);
        map.put("revisionList", revisionList);
        map.put("dmaterialList", dmaterialList);

        return map;
    }

    /**
     * 标准秒值详情
     */
    @Override
    public List<Standard> getStandardDetail(String model, String factory, String revision, String dmaterial) {
        return modelMapper.getStandardDetail(model, factory, revision, dmaterial);
    }

    /**
     * 直工 PPH 选项
     */
    @Override
    public Map<String, Object> getDirectOption(String model) {
        Map<String, Object> map = new HashMap<>();

        List<String> factoryList = modelMapper.getDirectFactory(model);
        List<String> yearList = modelMapper.getDirectYear(model);
        List<String> monthList = modelMapper.getDirectMonth(model);

        map.put("factoryList", factoryList);
        map.put("yearList", yearList);
        map.put("monthList", monthList);

        return map;
    }

    /**
     * 直工 PPH 详情
     */
    @Override
    public List<DirectPPH> getDirectPPH(String model, String factory, String year, String month) {
        return modelMapper.getDirectPPH(model, factory, year, month);
    }
}
